<%@page import="CloudStorage.*"%>
<%@page import="CloudStorage.File"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%
            int size = 0;
            Cart cart = (Cart) session.getAttribute("cart");
            if (session.getAttribute("cart") != null) {
                size = cart.getSize();
            }
            session.setAttribute("url", "downloads.jsp");
%>
<html>
    <head>
        <title>Cloud Share - Downloads</title>
        <link rel="SHORTCUT ICON" href="images/favicon.ico" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="style.css" />
        <link rel="stylesheet" type="text/css" href="search_style.css" />
        <script language="javascript">
            <!--

            var state = 'none';

            function showhide(layer_ref) {

                if (state == 'block') {
                    state = 'none';
                }
                else {
                    state = 'block';
                }
                if (document.all) { //IS IE 4 or 5 (or 6 beta)
                    eval( "document.all." + layer_ref + ".style.display = state");
                }
                if (document.layers) { //IS NETSCAPE 4 or below
                    document.layers[layer_ref].display = state;
                }
                if (document.getElementById &&!document.all) {
                    hza = document.getElementById(layer_ref);
                    hza.style.display = state;
                }
            }
            //-->
        </script>
        <style type="text/css">
            <!--
            #main .inside .block1 {
                text-align: left;
                clear: none;
                float: left;
                margin-left: 250px;
                height: 200px;
            }


            a:link {
                color: #F6F1F0;
                text-decoration: none;
            }
            a:visited {
                text-decoration: none;
            }
            a:hover {
                text-decoration: none;
            }
            a:active {
                text-decoration: none;
            }

            body,td,th {
                color: #64584C;
            }

            -->
            #blocks .inside .searchbar #search {
                margin-bottom: 0px;
                padding-top:3px;
            }
        </style>
    </head>

    <body>
        <div id="header">
            <div class="inside"> <a href="#" class="logo"><img src="images/logo2.gif" alt="" width="131" height="83" /></a>
                <p class="slogan">Share on Clouds!</p>
                <div id="cart">
                    <ul>
                        <li><a href="downloadcart.jsp"><img alt="cart" src="images/cart.png">  Download Cart (<%=size%>)</a> </li>
                    </ul>
                </div>
                <a href="faq.jsp" class="rss"><img src="images/rss.jpg" alt="" width="39" height="41" /></a>
            </div>
            <div id="menu">
                <% if (session.getAttribute("status") == null) {%>
                <ul>
                    <li><a href="index.jsp"><img src="images/but_home.jpg" alt="" width="148" height="40" /></a></li>
                    <li><a href="login.jsp"><img src="images/but_login.jpg" alt="" width="147" height="40" /></a></li>
                    <li><a href="register.jsp"><img src="images/but_reg.jpg" alt="" width="146" height="40" /></a></li>
                    <li><a href="downloads.jsp"><img src="images/but_down.jpg" alt="" width="146" height="40" /></a></li>
                    <li><a href="upload.jsp"><img src="images/but_upload.jpg" alt="" width="146" height="40" /></a></li>
                    <li><a href="buy.jsp"><img src="images/but_buy.jpg" alt="" width="146" height="40" /></a></li>

                </ul>
                <% } else {%>
                <ul>
                    <li><a href="index.jsp"><img src="images/but_home.jpg" alt="" width="148" height="40" /></a></li>
                    <li><a href="account.jsp"><img src="images/but_account.jpg" alt="" width="146" height="40" /></a></li>
                    <li><a href="downloads.jsp"><img src="images/but_down.jpg" alt="" width="146" height="40" /></a></li>
                    <li><a href="upload.jsp"><img src="images/but_upload.jpg" alt="" width="146" height="40" /></a></li>
                    <li><a href="buy.jsp"><img src="images/but_buy.jpg" alt="" width="146" height="40" /></a></li>
                    <li><a href="LogoutServlet"><img src="images/but_logout.jpg" alt="" width="147" height="40" /></a></li>
                </ul>
                <%}%>
            </div>
            <div class="headimage">
                <img src="images/picture.jpg" alt="" /> </div>
        </div>
        <div class="inside">

        </div>
        <div id="main">
            <div class="inside">
                <% Database database = new Database();
                            if (session.getAttribute("status") != null) {%>
                <div id="cpanel">
                    <h1><img src="images/cpanel.png" width="40" height="20">User panel</h1>
                    <ul>
                        <li></li>
                        <li><img src="images/myaccount-icon.png" width="20" height="20"> <a href="account.jsp">My Account</a></li>
                        <li><img src="images/uploads_icon.png" width="20" height="20" ><a href="myuploads.jsp"> My Uploads</a></li>
                        <li><img src="images/uploadicon.png" width="20" height="20"><a href="upload.jsp"> Upload File</a></li>
                        <li><img src="images/cart.png" width="20" height="20"><a href="downloadcart.jsp"> Download Cart</a></li>
                        <li><img src="images/buy_icon.png" alt="" width="20" height="20"><a href="buy.jsp">Buy more Space</a></li>
                    </ul>
                    <span><a href="LogoutServlet"> <img src="images/logout.png"/></a></span>
                </div>
                <%}%>
                <div id="blocks">
                    <div class="searchbar">
                        <h1>Search for Downloads</h1>
                        <div id="search-img">
                            <img src="images/search_img.gif" width="100" height="100">
                        </div>
                        <div id="search-text">
                            <form method="post" action="AdvancedSearchServlet?redirect=downloads.jsp" id="keyword">
                                <table>
                                    <tr>
                                        <td>Keyword</td>
                                        <td>
                                            <input name="keyword" type="text">
                                        </td>

                                    </tr>
                                 
                                       
                                        <tr>
                                            <td>
                                                <input name="submit" type="image" src="images/search.png">
                                            </td>
                                        </tr>
                                </table>
                            </form>
                        </div>
                    </div>
                </div>
                <div id="file-list">
                    <h2>Download Library</h2>
                    <% if (session.getAttribute("results") == null) {%>
                    <table width="100%" border="0">
                        <tr>
                            <td class="first" align="center">Date</td>
                            <td class="first" align="center">User</td>
                            <td class="first" align="center">Filename</td>
                            <td class="first" align="center">Category</td>
                            <td class="first" align="center">Type</td>
                            <td class="first" align="center">Price</td>
                            <td class="first" align="center">Downloads</td>
                            <td class="first" align="center">Rate of Popularity</td>
                            <td>&nbsp;</td>
                        </tr>
                        <%
                             ArrayList<File> files;

                             if (session.getAttribute("status") != null) {
                                 files = database.getRangedFiles(0, 15);
                             } else {
                                 files = database.getRangedPublicFiles(0, 15);
                             }
                             for (int i = 0; i < files.size(); i++) {
                                 int ownerId = files.get(i).getOwnerId();
                                 String username = database.getUsername(ownerId);

                        %>
                        <tr>
                            <td width="80" align="center"><%= files.get(i).getDatePublished()%></td>
                            <td width="80" align="center"><%= username%></td>
                            <td align="center"><a href="viewfile.jsp?fileid=<%=files.get(i).getID()%>"><%= files.get(i).getName()%></a></td>
                            <td align="center">
                                <%  if (files.get(i).getCategory().equalsIgnoreCase("video")) {
                                %>
                                <img src="images/video.gif" width="15" height="20">
                                <%} else if (files.get(i).getCategory().equalsIgnoreCase("document")) {
                                %>
                                <img src="images/doc.gif" width="15" height="20">
                                <%  } else if (files.get(i).getCategory().equalsIgnoreCase("music")) {
                                %>
                                <img src="images/music.gif" width="15" height="20">
                                <%  } else if (files.get(i).getCategory().equalsIgnoreCase("other")) {
                                %>
                                <img src="images/other.gif" width="15" height="20">
                                <%  }%>
                            </td>
                            <td align="center"><%= files.get(i).getStatus()%></td>
                            <td align="center"><%= files.get(i).getValue()%></td>
                            <td align="center"><%= files.get(i).getDownloads()%></td>
                            <td align="center"><%= ((float) files.get(i).getDownloads() / database.getDownloadsCounter()) * 100%>%</td>

                            <% User user = (User) session.getAttribute("user");
                                    if ( user == null || ( user != null && files.get(i).getOwnerId() != user.getId()) ) {
                           if (cart.contains(files.get(i).getID()) == false) {%>
                            <td><a href="AddToCartServlet?fileid=<%=files.get(i).getID()%>"><img src="images/cart_put.png"></a></td>
                                    <%} else {%>
                            <td><a href="DeleteFromCartServlet?fileid=<%=files.get(i).getID()%>"><img src="images/cart_remove.png"></a></td>
                                    <%}
                           }%>
                        </tr>
                        <%}%>
                    </table>
                    <a name="results"></a>

                    <%} else {
                         ArrayList<File> files = (ArrayList<File>) session.getAttribute("results");
                         if (files.size() == 0) {%><br/><h2>Search Results</h2>
                    <p>No results found!</p>

                    <%session.removeAttribute("results");
                                                     } else {%>
                    <br/><h2>Search Results</h2>
                    <table width="100%" border="0">
                        <tr>
                            <td class="first" align="center">Date</td>
                            <td class="first" align="center">User</td>
                            <td class="first" align="center">Filename</td>
                            <td class="first" align="center">Category</td>
                            <td class="first" align="center">Type</td>
                            <td class="first" align="center">Price</td>
                            <td class="first" align="center">Downloads</td>
                            <td class="first" align="center">Rate of Popularity</td>
                            <td>&nbsp;</td>
                        </tr>
                        <%
                               for (int i = 0; i < files.size(); i++)
                               {
                                    int ownerId = files.get(i).getOwnerId();
                                    String username = database.getUsername(ownerId);

                                    if ( files.get(i).getStatus().equals("Private") && session.getAttribute("status") == null )
                                        continue;
                        %>
                        <tr>
                            <td width="80" align="center"><%= files.get(i).getDatePublished()%></td>
                            <td width="80" align="center"><%= username%></td>
                            <td align="center"><a href="viewfile.jsp?fileid=<%=files.get(i).getID()%>"><%= files.get(i).getName()%></a></td>
                            <td align="center">
                                <%  if (files.get(i).getCategory().equalsIgnoreCase("video")) {
                                %>
                                <img src="images/video.gif" width="15" height="20">
                                <%} else if (files.get(i).getCategory().equalsIgnoreCase("document")) {
                                %>
                                <img src="images/doc.gif" width="15" height="20">
                                <%  } else if (files.get(i).getCategory().equalsIgnoreCase("music")) {
                                %>
                                <img src="images/music.gif" width="15" height="20">
                                <%  } else if (files.get(i).getCategory().equalsIgnoreCase("other")) {
                                %>
                                <img src="images/other.gif" width="15" height="20">
                                <%  }%>
                            </td>
                            <td align="center"><%= files.get(i).getStatus()%></td>
                            <td align="center"><%= files.get(i).getValue()%></td>
                            <td align="center"><%= files.get(i).getDownloads()%></td>
                            <td align="center"><%= ((float) files.get(i).getDownloads() / database.getDownloadsCounter()) * 100%>%</td>

                             <% User user = (User) session.getAttribute("user");
                                    if ( user == null || ( user != null && files.get(i).getOwnerId() != user.getId()) ) {
                           if (cart.contains(files.get(i).getID()) == false) {%>
                            <td><a href="AddToCartServlet?fileid=<%=files.get(i).getID()%>"><img src="images/cart_put.png"></a></td>
                                    <%} else {%>
                            <td><a href="DeleteFromCartServlet?fileid=<%=files.get(i).getID()%>"><img src="images/cart_remove.png"></a></td>
                                    <%}
                            }%>
                        </tr>
                        <%}
                                                     session.removeAttribute("results");
                        %>
                    </table>
                    <%}%>
                    <%}%>
                </div>
            </div>
        </div>

        <div id="footer">
            <div class="inside">
                <p>Cloud Share &copy; 2010   |  Webdesign by Panoylhs &amp; Roumdic S.A.</p>
            </div>
        </div>
    </body>
</html>
